package com.xing.msm.controller;

import com.alibaba.excel.util.StringUtils;
import com.xing.common.result.Result;

import com.xing.hospital.model.vo.msm.MsmVo;

import com.xing.msm.service.MsmService;
import com.xing.msm.utils.RondomUtil;
import com.xing.rabbitmq.config.MQConfig;
import com.xing.rabbitmq.constant.MqConst;
import com.xing.rabbitmq.service.RabbitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author 邢福豪
 * @date 2022/7/21 16:25
 */


@RestController
@RequestMapping("/api/msm")
public class MsmApiController {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    private MsmService msmService;

    @Autowired
    private RabbitService rabbitService;


    //发送手机验证码
    @GetMapping("send/{phone}")
    public Result sendCode(@PathVariable String phone) {
        //从redis获取验证码，如果获取到，返回ok
        //key 手机号 value 验证码
        String code = redisTemplate.opsForValue().get(phone + 1);
        if (!StringUtils.isEmpty(code)) {
            return Result.ok();
        }
        //如果验证码取不到，
        //生成验证码
        code = RondomUtil.getFourBitRandom();
        //调用service方法，通过整合短信服务进行发送
        boolean isSend = msmService.send(phone, code);
        //生成验证码放到redis里面，设置有效时间
        if (isSend) {
            redisTemplate.opsForValue().set(phone, code, 2, TimeUnit.MINUTES);
            return Result.ok();
        } else {
            return Result.fail().message("发送短信失败");
        }
    }


}
